package com.zryc.shop.ums.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.ums.entity.Menu;
import com.zryc.shop.ums.entity.Resource;
import com.zryc.shop.ums.entity.Role;
import com.zryc.shop.ums.service.IMenuService;
import com.zryc.shop.ums.service.IResourceService;
import com.zryc.shop.ums.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-16
 */
@RestController
@Api(tags = "角色管理接口列表")
@RequestMapping("/ums/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @Autowired
    private IResourceService resourceService;

    @PostMapping("/delete")
    @ApiOperation("删除角色")
    public Result delete(@ApiParam(value = "角色id", required = true) Integer ids) {
        return Result.ok(roleService.deleteRoleById(ids));
    }

    @PostMapping("/create")
    @ApiOperation("新增角色")
    public Result create(@RequestBody Role role) {
        roleService.save(role);
        return Result.ok("新增成功");
    }

    @PostMapping("/updateStatus/{id}")
    @ApiOperation("更新角色状态")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        roleService.updateRoleStatus(id, status);
        return Result.ok("更新成功");
    }


    @PostMapping("/update/{id}")
    @ApiOperation("更新角色信息")
    public Result update(@PathVariable @ApiParam(value = "角色id", required = true) Long id, @RequestBody Role role) {
        roleService.updateRole(id, role);
        return Result.ok("更新成功");
    }

    @GetMapping("/list")
    @ApiOperation("分页查询角色列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "姓名") String keyword,
                       @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "10") @ApiParam(value = "每页大小", required = true) Integer pageSize) {
        return Result.ok(roleService.list(keyword, pageNum, pageSize));
    }


    @GetMapping("/listAll")
    @ApiOperation("查询全部角色列表")
    public Result listAll() {
        return Result.ok(roleService.list());
    }

    @GetMapping("/listMenu/{roleId}")
    @ApiOperation("根据角色id查询菜单列表")
    public Result getMenuByRoleId(@PathVariable @ApiParam(value = "角色id", required = true) Long roleId) {
        Role role = roleService.getById(roleId);
        if (role == null) {
            return Result.error(400000, "角色不存在");
        }
        List<Menu> menus = menuService.getMenuByRoleId(roleId);
        return Result.ok(menus);
    }

    @PostMapping("/allocMenu")
    @ApiOperation("分配菜单")
    public Result allocMenu(@RequestParam Long roleId,@RequestParam List<Long> menuIds){
        return Result.ok(roleService.updateMenuForRole(roleId,menuIds));
    }

    @GetMapping("/listResource/{roleId}")
    @ApiOperation("根据角色id查询资源列表")
    public Result listResource(@PathVariable @ApiParam(value = "角色id", required = true) Long roleId) {
        Role role = roleService.getById(roleId);
        if (role == null) {
            return Result.error(400000, "角色不存在");
        }
        List<Resource> resources = resourceService.getResourceByRoleId(roleId);
        return Result.ok(resources);
    }


    @PostMapping("/allocResource")
    @ApiOperation("分配资源")
    public Result allocResource(@RequestParam Long roleId,@RequestParam List<Long> resourceIds){
        return Result.ok(roleService.updateResourceForRole(roleId,resourceIds));
    }
}
